define(['zepto', 'butterfly/view','../../bsl',
    'text!./storage.html'
], function($, View, bsl, storage) {
    var me = null;

    var view = View.extend({
        events: {

            // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
            "tap #openDoc" : "OpenDoc",
            "tap #back" : "back",
            "tap .setItemFun" : "setItemFun",
            "tap .getAllItem" : "getAllItem",
            "tap .modifyTopFive" : "modifyTopFive",
            "tap .delItem" : "delItem",
            "tap .clearAllItem" : "clearAllItem",

      
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           


        },
         setItemFun:function ( id ) {
            //循环插入100调数据
            var dataNum = new Number(id);
            for ( var i=0; i<dataNum; i++ ) {
                bsl.storage.setItem( "key"+i, "value"+i );
            }
            var length = getLengthFun();
            console.info( "数据存储成功，存储了"+length+"条数据；" );
        },
        getLengthFun:function (){
            return bsl.storage.getLength();
        },
        getAllItem:function (){
            console.info( "获取数据：" );
            var itemLength = getLengthFun();
            for ( var i=0; i < itemLength; i++ ) {
                var key = bsl.storage.key(i);
                var value = bsl.storage.getItem(key);
                console.info( "key:"+key+"-->value:"+value );
            };
        },
       delItem: function () {
            console.info( "删除数据：" );
            var itemLength = getLengthFun();
            for (var i=0; i < itemLength; i+=2) {
                var key = bsl.storage.key(i);
                bsl.storage.removeItem(key);
            };
            var itemNo = getLengthFun();
            console.info( "移除了"+(itemLength-itemNo)+"条数据" );
        },
        clearAllItem:function (){
            bsl.storage.clear();
            var num = getLengthFun();
            if ( num == 0 ) {
                console.info( "storage数据清除成功！" );
            }else{
                console.info( "storage数据清除失败！" );
            }
        },
        modifyTopFive:function (){
            console.info( "修改前的数据是：" );
            for (var i=0; i < 5; i++) {
                var key = bsl.storage.key(i),value = bsl.storage.getItem(key);
                console.info( "key:"+key+"-->value:"+value );
                bsl.storage.setItem( key, "新数据值>>"+i );
            };
            console.info( "修改后的数据是：" );
            for (var i=0; i < 5; i++) {
                var key = bsl.storage.key(i),value = bsl.storage.getItem(key);
                console.info( "key:"+key+"-->value:"+value );
            };
        },

        back : function(e){
                    window.history.go(-1);
                },
        OpenDoc : function(e){
           
            window.butterfly.navigate('com.no5.cordovademo/doc/storage', {
                trigger: true
            });
        },
      
            });
    return view;
});
